HashMap

Associative array / hash map.

Disabled Default Constructor

A disabled default is present on this object. To use it, use one of the other constructors or a factory function.

Constructors

this
this(Allocator allocator)

Use the given allocator for allocations.

this
this(size_t bucketCount, Allocator allocator)

Constructs an HashMap with an initial bucket count of bucketCount. bucketCount must be a power of two.

this
this(size_t bucketCount)

Constructs an HashMap with an initial bucket count of bucketCount. bucketCount must be a power of two.

Destructor

A destructor is present on this object, but not explicitly documented in the source.

Postblit

Copying this object is disabled.

A postblit is present on this object, but not explicitly documented in the source.

Members

Aliases

opSlice
alias opSlice = byValue

Functions

byKey
auto byKey()
byKeyValue
auto byKeyValue()
byValue
auto byValue()
clear
void clear()

Removes all items from the map

containsKey
bool containsKey(K key)
empty
bool empty()
get
auto get(K key, lazy V defaultValue)

Gets the value for the given key, or returns defaultValue if the given key is not present.

getOrAdd
auto getOrAdd(K key, lazy V defaultValue)

If the given key does not exist in the HashMap, adds it with the value defaultValue.

keys
K[] keys()
length
size_t length()
opBinaryRight
inout(V)* opBinaryRight(const K key)

Supports key in aa syntax.

opIndex
ref opIndex(K key)

Supports aa[key] syntax.

opIndexAssign
void opIndexAssign(V value, const K key)

Supports aakey = value; syntax.

remove
bool remove(K key)

Removes the value associated with the given key

values
auto values()

Examples

1 import std.uuid : randomUUID;
2 import std.algorithm.iteration : walkLength;
3 
4 auto hm = HashMap!(string, int)(16);
5 assert (hm.length == 0);
6 assert (!hm.remove("abc"));
7 hm["answer"] = 42;
8 assert (hm.length == 1);
9 assert ("answer" in hm);
10 hm.remove("answer");
11 assert (hm.length == 0);
12 hm["one"] = 1;
13 hm["one"] = 1;
14 assert (hm.length == 1);
15 assert (hm["one"] == 1);
16 hm["one"] = 2;
17 assert(hm["one"] == 2);
18 foreach (i; 0 .. 1000)
19 {
20 	hm[randomUUID().toString] = i;
21 }
22 assert (hm.length == 1001);
23 assert (hm.keys().length == hm.length);
24 assert (hm.values().length == hm.length);
25 () @nogc {
26 	assert (hm.byKey().walkLength == hm.length);
27 	assert (hm.byValue().walkLength == hm.length);
28 	assert (hm[].walkLength == hm.length);
29 	assert (hm.byKeyValue().walkLength == hm.length);
30 }();
31 foreach (v; hm) {}
32 
33 auto hm2 = HashMap!(char, char)(4);
34 hm2['a'] = 'a';
35 
36 HashMap!(int, int) hm3;
37 assert (hm3.get(100, 20) == 20);
38 hm3[100] = 1;
39 assert (hm3.get(100, 20) == 1);
40 auto pValue = 100 in hm3;
41 assert(*pValue == 1);

Meta